/* PexifTool - $Revision: 18 $
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	$Id: AboutDialog.java 18 2011-10-06 21:32:15Z petojan@googlemail.com $
 */
package de.pd.pjim2.controller.gui.dialog;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.net.URI;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.swingx.JXHyperlink;

import de.pd.pjim2.controller.PJWindowController;

/**
 * @author petros
 *
 */
@SuppressWarnings("serial")
public class AboutDialog extends JDialog {

	private final JPanel mcontentPanel = new JPanel();
	/**
	 * Create the dialog.
	 */
	public AboutDialog(JFrame pFrame) {
		super(pFrame, true);
		
		ResourceMap resourceMap = Application.getInstance(PJWindowController.class).getContext().getResourceMap(PJWindowController.class);
		ApplicationActionMap actionMap = Application.getInstance().getContext()
				.getActionMap(this);
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		setTitle("About");
		
		mcontentPanel.setLayout(new MigLayout("wrap 2"));
		
		// the icon
		JLabel lblIcon = new JLabel(resourceMap.getIcon("Application.icon"));
		mcontentPanel.add(lblIcon, "");
		
		String title = resourceMap.getString("Application.title");
		JLabel lblAppTitle = new JLabel("<html><h2>" + title + "</h2>");
		mcontentPanel.add(lblAppTitle);
		
		// vendor
		JLabel lblVendor = new JLabel("<html><b>Vendor</b></html>");
		mcontentPanel.add(lblVendor);
		JLabel lblVendor2 = new JLabel(resourceMap.getString("Application.vendor"));
		mcontentPanel.add(lblVendor2);
		
		// website
		JLabel lblWebsite = new JLabel("<html><b>Homepage</b></html>");
		mcontentPanel.add(lblWebsite);
		JXHyperlink lblWebsite2 = new JXHyperlink(actionMap.get("openWebsiteAction"));
		lblWebsite2.setText("www.petrosit.de");
		mcontentPanel.add(lblWebsite2);
		
		getContentPane().add(mcontentPanel, BorderLayout.CENTER);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton(actionMap.get("okAction"));
				okButton.setText("Close");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
				
				JButton donateButton = new JButton(actionMap.get("openDonationUrlAction"));
				donateButton.setText("Donate");
				buttonPane.add(donateButton);
			}
		}
	}
	
	@Action
	public void okAction() {
		dispose();
	}
	
	@Action
	public void openWebsiteAction() {
		String uri = "http://www.petrosit.de";
		openURL(uri);
	}

	/**
	 * @param uri
	 */
	private void openURL(String uri) {
		if(Desktop.isDesktopSupported()) {
			try {
				Desktop.getDesktop().browse(new URI(uri));
			} catch (Exception ex) {
			}
		}
	}
	
	@Action
	public void openDonationUrlAction() {
		openURL("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=L5TJXLXZFLLDC");
	}
}
